package com.damie.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.dadie.Domain.ShopDomain;
import com.dadie.Domain.ShopShelvesDomain;
import com.dadie.Dto.ShopDto;
import com.dadie.parame.ShopShelvesParame;
import com.damie.mapper.ShopMapper;
import com.damie.mapper.ShopShelvesMapper;
import com.damie.service.ShopShelvesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.damie.uilt.Copy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-23
 */
@Service
public class ShopShelvesServiceImpl extends ServiceImpl<ShopShelvesMapper, ShopShelvesDomain> implements ShopShelvesService {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private ShopShelvesMapper shopShelvesMapper;

    @Transactional
    @Override
    public ShopDto addProductShelves(ShopShelvesParame shopShelvesParame) {
        QueryWrapper<ShopDomain> shopDomainQueryWrapper = new QueryWrapper<>();
        shopDomainQueryWrapper.eq(shopShelvesParame.getProductId() != null, "product_id", shopShelvesParame.getProductId());
        ShopDomain shopDomain = shopMapper.selectOne(shopDomainQueryWrapper);
        if (ObjectUtils.isNotEmpty(shopDomain)){
            if (shopDomain.getProductInventory()<shopShelvesParame.getProductUpNum()){
                throw new RuntimeException("参数异常，数量超过库存");
            }
//            Long size = redisTemplate.opsForList().size("productId:" + shopShelvesParame.getProductId());
            QueryWrapper<ShopShelvesDomain> shopShelvesDomainQueryWrapper = new QueryWrapper<>();
            shopShelvesDomainQueryWrapper.eq("product_id",shopShelvesParame.getProductId());
            List<ShopShelvesDomain> shopShelvesDomains = shopShelvesMapper.selectList(shopShelvesDomainQueryWrapper);
            int size = shopShelvesDomains.size();
            //判断该商品有无上架
            if (size>0){
                //上架完成扣减库数量
                shopDomain.setProductInventory(shopDomain.getProductInventory()-shopShelvesParame.getProductUpNum());
                shopMapper.updateById(shopDomain);
                //设定上架数据
                ShopShelvesDomain shopShelvesDomain = new ShopShelvesDomain();
                if (shopShelvesParame.getProductUpName()!=null&&shopShelvesParame.getProductUpName()!=""){
                    shopShelvesDomain.setProductUpName(shopShelvesParame.getProductUpName());
                }else {
                    shopShelvesDomain.setProductUpName(shopDomain.getProductName());
                }
                shopShelvesDomain.setProductUpAcon(shopShelvesParame.getProductUpAcon());
                shopShelvesDomain.setProductUpNum(shopShelvesParame.getProductUpNum());
                shopShelvesDomain.setProductId(shopDomain.getProductId());
                shopShelvesMapper.insert(shopShelvesDomain);
                //返回前端值上架数据
                shopDomain.setProductInventory(shopShelvesParame.getProductUpNum());
                shopDomain.setProductTotal(shopShelvesParame.getProductUpAcon());
                ShopDto copy = (ShopDto)Copy.copy(shopDomain,new ShopDto());
                return copy;
            }else {//已经上架则继续新增
                //上架完成扣减库数量
                shopDomain.setProductInventory(shopDomain.getProductInventory()-shopShelvesParame.getProductUpNum());
                shopMapper.updateById(shopDomain);

                ShopShelvesDomain shopShelvesDomain = new ShopShelvesDomain();
                if (shopShelvesParame.getProductUpName()!=null&&shopShelvesParame.getProductUpName()!=""){
                    shopShelvesDomain.setProductUpName(shopShelvesParame.getProductUpName());
                }else {
                    shopShelvesDomain.setProductUpName(shopDomain.getProductName());
                }

                shopShelvesDomain.setProductUpAcon(shopShelvesParame.getProductUpAcon());
                shopShelvesDomain.setProductUpNum(shopShelvesParame.getProductUpNum());
                shopShelvesDomain.setProductId(shopDomain.getProductId());
                shopShelvesMapper.insert(shopShelvesDomain);

                shopDomain.setProductInventory(shopShelvesParame.getProductUpNum());
                shopDomain.setProductTotal(shopShelvesParame.getProductUpAcon());
                ShopDto copy = (ShopDto)Copy.copy(shopDomain, new ShopDto());
                return copy;
            }
        }
        throw new RuntimeException("没有该商品");
    }

    @Transactional
    @Override
    public ShopDto downProductShelves(String id) {
        ShopShelvesDomain shopShelvesDomain = shopShelvesMapper.selectById(id);
        if (ObjectUtils.isNotEmpty(shopShelvesDomain)){
            Integer productUpNum = shopShelvesDomain.getProductUpNum();
            ShopDomain shopDomain = shopMapper.selectById(shopShelvesDomain.getProductId());
            shopDomain.setProductInventory(shopDomain.getProductInventory()+productUpNum);
            shopMapper.updateById(shopDomain);
            shopShelvesMapper.deleteById(id);
            return(ShopDto) Copy.copy(shopDomain,new ShopDto());
        }
        throw new RuntimeException("删除的商品不存在");
    }
}
